package controllers

import (
	"fmt"
	"game_go/base/util"
	"game_go/dao/game"
	"log"
	"os"

	"time"

	"github.com/tealeg/xlsx"
)

type MainController struct {
	BaseController
}

var (
	nStop int64 = int64(0)
)

func (c *MainController) GameList() {
	envelope := c.envelope
	defer c.ServeJSON()

	sName := c.GetPostValue("name")
	sProc := c.GetPostValue("proc")

	sAcc := c.GetPostValue("acc")

	nUsed, err := c.GetPostInt64("used", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "proc"
		return
	}
	nDown, err := c.GetPostInt64("locked", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "proc"
		return
	}
	sToken := c.GetPostValue("token")
	nDone, err := c.GetPostInt64("done", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "proc"
		return
	}

	nCurrent, err := c.GetPostInt64("cp", 1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "cp"
		return
	}
	nPageSize, err := c.GetPostInt64("sz", 25)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "sz"
		return
	}

	mData, err := game.QueryGameList(sName, sAcc, sToken, sProc, nUsed, nDown, nDone, (nCurrent-1)*nPageSize, nPageSize)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}
	if mData == nil {
		envelope.Data = util.EmptyMap()
	} else {
		envelope.Data = mData
	}
}

//停机
func (c *MainController) Downtime() {
	envelope := c.envelope
	defer c.ServeJSON()
	bStop, err := c.GetPostInt64("stop", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "stop"
		return
	}
	if bStop == -1 {
		envelope.Result = false
		envelope.ErrMsg = "stop is nil"
		return
	}
	nStop = bStop
	envelope.Data = nStop
}

//当前状态
func (c *MainController) GetDownTime() {
	envelope := c.envelope
	envelope.Data = nStop
	c.ServeJSON()
}

//上传
func (c *MainController) UpdateLoad() {
	envelope := c.envelope
	defer c.ServeJSON()
	file, head, err := c.GetFile("xlsxFile")
	if err == nil {
		//关闭文件
		file.Close()
	} else if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "文件上传失败"
		return
	}

	//设置保存文件名
	sFileName := fmt.Sprintf("%s", head.Filename)
	log.Println("fileName:", sFileName)
	//将文件保存到服务器中
	err = c.SaveToFile("xlsxFile", sFileName)
	if err != nil {
		log.Println(err)
		envelope.ErrMsg = "保存文件错误"
		envelope.Result = false
		return
	}

	gameInfos, err := parseXlsxFile(sFileName)
	if err != nil {
		log.Println(err)
		envelope.ErrMsg = "文件解析错误"
		envelope.Result = false
		return
	}

	if len(gameInfos) == 0 {
		envelope.ErrMsg = "游戏信息为空"
		envelope.Result = false
		return
	}

	//解析完文件删掉
	err = os.Remove(sFileName)
	if err != nil {
		log.Println(err)
	}
	err = game.InsertGameInfo(gameInfos)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "数据库错误"
		return
	}
}

//下发
func (c *MainController) Issued() {
	envelope := c.envelope
	defer c.ServeJSON()
	sName := c.GetPostValue("name")
	sProc := c.GetPostValue("proc")

	if sName == "" || sProc == "" {
		envelope.Result = false
		envelope.ErrMsg = "参数不全"
		return
	}

	if nStop == 1 {
		envelope.ErrMsg = "stop"
		envelope.Result = false
		return
	}

	mData, err := game.QueryIssued(sName, sProc)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "数据库错误"
		return
	}
	if mData == nil {
		envelope.Data = util.EmptyMap()
	} else {
		envelope.Data = mData
	}
}

//回发
func (c *MainController) Upload() {
	envelope := c.envelope
	defer c.ServeJSON()
	sName := c.GetPostValue("name")
	sProc := c.GetPostValue("proc")
	sAcc := c.GetPostValue("acc")
	sToken := c.GetPostValue("token")
	nGold, err := c.GetPostInt64("gold", 0)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "gold"
		return
	}

	if sName == "" || sAcc == "" || sProc == "" {
		envelope.ErrMsg = "参数不全"
		envelope.Result = false
		return
	}

	err = game.UpdateInfo(sName, sAcc, sToken, sProc, nGold)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}
}

//导出
func (c *MainController) Export() {
	envelope := c.envelope
	defer c.ServeJSON()

	sName := c.GetPostValue("name")
	sProc := c.GetPostValue("proc")

	mData, err := game.Export(sName, sProc)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}

	if mData == nil {
		envelope.Result = false
		envelope.ErrMsg = "no data"
		return
	}
	var file *xlsx.File
	var sheet *xlsx.Sheet
	var row *xlsx.Row
	var cell *xlsx.Cell

	file = xlsx.NewFile()
	sheet, err = file.AddSheet("Sheet1")
	if err != nil {
		fmt.Printf(err.Error())
	}

	for _, value := range mData {
		fmt.Println(value)
		row = sheet.AddRow()
		if value != nil {
			cell = row.AddCell()
			cell.Value = value["name"].(string)

			cell = row.AddCell()
			cell.Value = value["acc"].(string)

			cell = row.AddCell()
			cell.Value = value["pwd"].(string)

			cell = row.AddCell()
			nGold, ok := value["gold"].(int64)
			if !ok {
				log.Println("gold type fail")
			}
			cell.Value = fmt.Sprintf("%d", nGold)
		}
	}

	fileName := fmt.Sprintf("%d.xlsx", time.Now().UnixNano())
	err = file.Save(fileName)
	if err != nil {
		log.Println(err)
		envelope.ErrMsg = "file"
		envelope.Result = false
		return
	}

	err = game.UpdateExport(mData)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}
	envelope.Data = fileName
}

//查询
func (c *MainController) Query() {
	envelope := c.envelope
	defer c.ServeJSON()
	sName := c.GetPostValue("name")
	sProc := c.GetPostValue("proc")
	sAcc := c.GetPostValue("acc")
	sPwd := c.GetPostValue("pwd")
	nUsed, err := c.GetPostInt64("used", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "used"
		return
	}
	nlocked, err := c.GetPostInt64("locked", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "locked"
		return
	}
	sToken := c.GetPostValue("token")
	nDone, err := c.GetPostInt64("done", -1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "done"
		return
	}
	nCurrentPage, err := c.GetPostInt64("cp", 1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "cp"
		return
	}
	nPageSize, err := c.GetPostInt64("sz", 25)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "sz"
		return
	}

	mData, err := game.QueryBy(sName, sAcc, sPwd, sToken, sProc, nUsed, nlocked, nDone, (nCurrentPage-1)*nPageSize, nPageSize)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}
	if mData == nil {
		envelope.Data = util.EmptyArray()
	} else {
		envelope.Data = mData
	}
}

// 基本信息
func (c *MainController) InsertBasic() {
	envelope := c.envelope
	defer c.ServeJSON()
	gameName := c.GetPostValue("gameName")
	machineName := c.GetPostValue("machineName")
	region := c.GetPostValue("region")
	gold, err := c.GetPostInt64("gold", 0)
	if err != nil {
		envelope.Result = false
		envelope.ErrMsg = err.Error()
		log.Panicln(err)
		return
	}
	if len(gameName) == 0 || len(machineName) == 0 || len(region) == 0 {
		envelope.Result = false
		return
	}
	if _, err = game.InsertBasic(gameName,machineName, region, gold); err != nil {
		envelope.Result = false
		envelope.ErrMsg = err.Error()
		log.Panicln(err)
		return
	}
}

func (c *MainController) GetBasic() {
	envelope := c.envelope
	defer c.ServeJSON()
	gameName := c.GetPostValue("gameName")
	machineName := c.GetPostValue("machineName")
	region := c.GetPostValue("region")
	nCurrentPage, err := c.GetPostInt64("cp", 1)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "cp"
		return
	}
	nPageSize, err := c.GetPostInt64("sz", 25)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "sz"
		return
	}

	mData, err := game.QueryBasic(gameName,machineName, region, (nCurrentPage-1)*nPageSize, nPageSize)
	if err != nil {
		log.Println(err)
		envelope.Result = false
		envelope.ErrMsg = "database"
		return
	}
	if mData == nil {
		envelope.Data = util.EmptyArray()
	} else {
		envelope.Data = mData
	}
}
